﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model;
using l1cs.server.model.npc;

namespace l1cs.server.serverpackets
{
    public class S_NPCTalkReturn : ServerBasePacket
    {
        private byte[] _byte = null;

        public S_NPCTalkReturn( L1NpcTalkData npc, int objid, int action,
                String[] data )
        {

            String htmlid = "";

            if ( action == 1 )
            {
                htmlid = npc.getNormalAction();
            }
            else if ( action == 2 )
            {
                htmlid = npc.getCaoticAction();
            }
            else
            {
                throw new ArithmeticException();
            }

            buildPacket( objid, htmlid, data );
        }

        public S_NPCTalkReturn( L1NpcTalkData npc, int objid, int action )
            : this( npc, objid, action, null ) { }

        public S_NPCTalkReturn( int objid, String htmlid, String[] data )
        {
            buildPacket( objid, htmlid, data );
        }

        public S_NPCTalkReturn( int objid, String htmlid )
        {
            buildPacket( objid, htmlid, null );
        }

        public S_NPCTalkReturn( int objid, L1NpcHtml html )
        {
            buildPacket( objid, html.getName(), html.getArgs() );
        }

        private void buildPacket( int objid, String htmlid, String[] data )
        {

            writeC( Opcodes.S_OPCODE_SHOWHTML );
            writeD( objid );
            writeS( htmlid );
            if ( data != null && 1 <= data.Length )
            {
                writeH( 0x01 ); // 不明バイト 分かる人居たら修正願います
                writeH( data.Length ); // 引数の数
                foreach ( String datum in data )
                {
                    writeS( datum );
                }
            }
            else
            {
                writeH( 0x00 );
                writeH( 0x00 );
            }
        }

        override
        public byte[] getContent()
        {
            if ( _byte == null )
            {
                _byte = _bao.ToArray();
            }
            return _byte;
        }

        override
        public String getType()
        {
            return "[S] _S__25_TalkReturn";
        }
    }
}
